<?php
/**
 * 批量发送邮件(有记录上一次发送的时间功能)
 * 读文件中的邮件,存在数组里了
 * 每次发一封
 */
//exit("处理好");
include_once "send_email_function.php";
set_time_limit(0);
ini_set('date.timezone', "Asia/Shanghai");
$s8 = microtime(true);
$fp = fopen("status_".date("Ymd_H_i_s").".txt", "a");//将当前的状态写入文件
//验证是否执行(每次执行是上次的20分钟)
$save_file_name = "data/time.txt";

$file_content = readFileContent($save_file_name);
$array = (unserialize($file_content));
//print_r($array);exit;
if(!empty($array)){
	if(time(true)-strtotime($array["start_execute_time"]) < 1200){//时间在大于20分钟,1200
		fwrite($fp, "执行时间小于20分钟".json_encode($array));//写入日志
		exit("执行时间小于20分钟");
	}else{
		$array["start_execute_time"] = date("Y-m-d H:i:s");
		saveFile((serialize($array)), $save_file_name);
	}
}else{
	$array = array(
		"start_execute_time"=>date("Y-m-d H:i:s"),//开始执行的时间
		"data_count"=>"",//执行到了第几个文件
		"data_key"=>0,//第几个key
		"data_email"=>"",//邮件地址是多少
	);
	saveFile((serialize($array)), $save_file_name);
}


//发件箱信息
$host_email_address = array(
	array("Host"=>"127.0.0.1","userNmae"=>"123234@kciwo93834.com","pwd"=>"123456","displayName"=>"周一0"),
	array("Host"=>"127.0.0.1","userNmae"=>"143er34@kciwo93834.com","pwd"=>"123456","displayName"=>"周二1"),
	array("Host"=>"127.0.0.1","userNmae"=>"14r53434r@kciwo93834.com","pwd"=>"123456","displayName"=>"周一2"),
	array("Host"=>"127.0.0.1","userNmae"=>"1er32@kciwo93834.com","pwd"=>"123456","displayName"=>"周二3"),
	array("Host"=>"127.0.0.1","userNmae"=>"1ergter@kciwo93834.com","pwd"=>"123456","displayName"=>"周一4"),
	array("Host"=>"127.0.0.1","userNmae"=>"1tuous@kciwo93834.com","pwd"=>"123456","displayName"=>"周二5"),
	array("Host"=>"127.0.0.1","userNmae"=>"22sfdfs7@kciwo93834.com","pwd"=>"123456","displayName"=>"周一6"),
	array("Host"=>"127.0.0.1","userNmae"=>"2dfsd4@kciwo93834.com","pwd"=>"123456","displayName"=>"周二7"),
	array("Host"=>"127.0.0.1","userNmae"=>"2dfsdf6@kciwo93834.com","pwd"=>"123456","displayName"=>"周一8"),
	array("Host"=>"127.0.0.1","userNmae"=>"2fxc3@kciwo93834.com","pwd"=>"123456","displayName"=>"周二9"),
	array("Host"=>"127.0.0.1","userNmae"=>"2sdfs5@kciwo93834.com","pwd"=>"123456","displayName"=>"周一10"),
	array("Host"=>"127.0.0.1","userNmae"=>"2vxcv2@kciwo93834.com","pwd"=>"123456","displayName"=>"周一02"),
	array("Host"=>"127.0.0.1","userNmae"=>"2xcv1@kciwo93834.com","pwd"=>"123456","displayName"=>"周二12"),
	array("Host"=>"127.0.0.1","userNmae"=>"2xcvsd8@kciwo93834.com","pwd"=>"123456","displayName"=>"周一22"),
	array("Host"=>"127.0.0.1","userNmae"=>"ansikc@kciwo93834.com","pwd"=>"123456","displayName"=>"周二32"),
/*	array("Host"=>"127.0.0.1","userNmae"=>"dadapao@kciwo93834.com","pwd"=>"123456","displayName"=>"周一42"),
	array("Host"=>"127.0.0.1","userNmae"=>"dialeda@kciwo93834.com","pwd"=>"123456","displayName"=>"周二52"),
	array("Host"=>"127.0.0.1","userNmae"=>"diaoyudao@kciwo93834.com","pwd"=>"123456","displayName"=>"周一62"),
	array("Host"=>"127.0.0.1","userNmae"=>"kc1215@kciwo93834.com","pwd"=>"123456","displayName"=>"周二72"),
	array("Host"=>"127.0.0.1","userNmae"=>"maimaimai@kciwo93834.com","pwd"=>"123456","displayName"=>"周一82"),
	array("Host"=>"127.0.0.1","userNmae"=>"makaili@kciwo93834.com","pwd"=>"123456","displayName"=>"周二92"),
	array("Host"=>"127.0.0.1","userNmae"=>"qianlong@kciwo93834.com","pwd"=>"123456","displayName"=>"周一102"),
	array("Host"=>"127.0.0.1","userNmae"=>"tuliuda@kciwo93834.com","pwd"=>"123456","displayName"=>"周一02"),
	array("Host"=>"127.0.0.1","userNmae"=>"zhangguoli1990@kciwo93834.com","pwd"=>"123456","displayName"=>"周二12"),
	array("Host"=>"127.0.0.1","userNmae"=>"zhougan@kciwo93834.com","pwd"=>"123456","displayName"=>"周一22"),
*/
);
$host_email_address_count = count($host_email_address);

$success = 0;$faile = 0;
$sum_send = 0;//共发送多少封

for ($i = 1; $i <= 22; $i++) {
	if(!empty($array["data_count"]) && $array["data_count"]>$i)continue;
	$email_all_array = include_once "data/data_{$i}.php";
	foreach ($email_all_array as $key => $email) {
		if($array["data_key"] > $key)continue;
		//发一个邮件写一定文件
		$array["data_key"]=$key;
		$array["data_email"]=$email;
		saveFile((serialize($array)), $save_file_name);
	
		//每个邮件的标题都不一样
		$title = "全方位_大空间_展示图片_".rand(100000,999999)."_".$email;
		$keyEmail = ($sum_send%$host_email_address_count);
		if($host_email_address[$keyEmail]["userNmae"] == "ansikc@kciwo93834.com")
			exit;
		$flage = send_email_function($host_email_address[$keyEmail],array($email),$title,getBody($email));//发送邮件
		$sum_send++;
		$end8 = date("Y-m-d H:i:s");
		//echo $flage;exit;
		if($flage){
			$success++;
		}else{
			$faile++;
			sleep(1);
		}
		if($sum_send%1 == 0){
			//每发100封记一次日志
			fwrite($fp, "成功:{$success},失败:{$faile},时间:{$end8},{$email}");//写入日志
		}
	}
	
	//执行下一个文件
	$array["data_count"]++;
	$array["data_key"]=0;
	$array["data_email"]="";
	saveFile((serialize($array)), $save_file_name);
}
fwrite($fp, "成功:{$success},失败:{$faile},时间:{$end8},{$email}");//写入日志
echo "success:".$success."faile:".$faile;
echo "<br>time:".number_format(microtime(true) - $s8, 3, '.', '')."s";
